




function formatNumberText(num) {
  if (Number.isInteger(num)) {
    return num.toString(); // 整数直接返回字符串形式
  } else {
    return num.toFixed(2); // 小数保留 2 位
  }
}



// 这里是userenegy页面的引入
export const getHour = function (hour) {
  let arr = hour.split(':')
  return arr[0] + ':' + arr[1]
}

export const userEnengy = function () {
  const now = new Date();
  let hour = now.getHours()
  const currentHour = now.getHours().toString().padStart(2, '0');

  // 生成24小时数组
  const timeSlots = Array.from({ length: hour + 1 }, (_, i) => {
    let hour = i.toString().padStart(2, '0');
    hour = hour + ':00'
    return [hour, hour === currentHour ? 0 : 0];
  });
  return timeSlots;
}



// 获取白班补全全天
export const generateHourDaylyTimeRange = function (startTime, endTime) {
  const result = [];

  // 解析时间字符串为分钟数
  const parseTime = (timeStr) => {
    const [hours, minutes] = timeStr.split(':').map(Number);
    return hours * 60 + minutes;
  };

  // 格式化分钟数为HH:mm
  const formatTime = (minutes) => {
    const hrs = Math.floor(minutes / 60) % 24;
    return `${String(hrs).padStart(2, '0')}:00`;
  };

  const startMins = parseTime(startTime);
  const endMins = parseTime(endTime);

  // 计算第一个整点时间（向上取整）
  const firstHour = startMins % 60 === 0 ? startMins : startMins + (60 - startMins % 60);

  // 计算最后一个整点时间（向下取整）
  const lastHour = endMins - (endMins % 60);

  // 判断是否是同一天
  const isSameDay = startMins <= endMins;

  // 生成时间点
  let current = firstHour;
  while (true) {
    const currentTime = current % (24 * 60);

    // 添加条件判断是否应该包含当前时间点
    if (isSameDay) {
      if (currentTime > lastHour) break;
    } else {
      if (currentTime > lastHour && current > endMins) break;
    }

    result.push([formatTime(currentTime), 0, 0]);

    current += 60;

    // 防止无限循环（最多24小时）
    if (current - firstHour > 24 * 60) break;
  }

  return result;
}
// 获取夜班补全
export const generateNightTimeArray = function (startTime, endTime) {
  const result = [];

  // 解析时间字符串为分钟数
  const parseTime = (timeStr) => {
    const [hours, minutes] = timeStr.split(':').map(Number);
    return hours * 60 + minutes;
  };

  // 格式化分钟数为HH:mm
  const formatTime = (minutes) => {
    const hrs = Math.floor(minutes / 60) % 24;
    return `${String(hrs).padStart(2, '0')}:00`;
  };

  const startMins = parseTime(startTime);
  const endMins = parseTime(endTime);

  // 计算第一个整点时间（向上取整）
  let firstHour = startMins;
  if (startMins % 60 !== 0) {
    firstHour = startMins + (60 - startMins % 60);
  }

  // 计算最后一个整点时间（向下取整）
  const lastHour = endMins - (endMins % 60);

  // 生成时间点
  let current = firstHour;
  while (true) {
    // 处理跨天情况
    const currentTime = current % (24 * 60);

    // 添加当前时间点到结果数组
    result.push([formatTime(currentTime), 0, 0]);

    // 检查是否到达结束时间
    if (currentTime === lastHour) break;

    current += 60;

    // 防止无限循环（最多24小时）
    if (result.length >= 24) break;
  }

  return result;
}


// 获取白班当前补全
export const currentTimeWOrk = function (fromTime) {
  const result = [];

  // 获取当前时间并格式化为HH:mm
  const getCurrentTime = () => {
    const now = new Date();
    return `${String(now.getHours()).padStart(2, '0')}:${String(now.getMinutes()).padStart(2, '0')}`;
  };

  // 解析时间字符串为分钟数
  const parseTime = (timeStr) => {
    const [hours, minutes] = timeStr.split(':').map(Number);
    return hours * 60 + minutes;
  };

  // 格式化分钟数为HH:mm
  const formatTime = (minutes) => {
    const hrs = Math.floor(minutes / 60) % 24;
    return `${String(hrs).padStart(2, '0')}:00`;
  };

  const fromMins = parseTime(fromTime);
  const currentTime = getCurrentTime();
  const currentMins = parseTime(currentTime);

  // 计算第一个整点时间（向上取整）
  const firstHour = fromMins % 60 === 0 ? fromMins : fromMins + (60 - fromMins % 60);

  // 计算最后一个整点时间（向下取整）
  const lastHour = currentMins - (currentMins % 60);

  // 判断是否是同一天
  const isSameDay = fromMins <= currentMins;

  // 生成时间点
  let current = firstHour;
  while (true) {
    const currentTime = current % (24 * 60);

    // 添加条件判断是否应该包含当前时间点
    if (isSameDay) {
      if (currentTime > lastHour) break;
    } else {
      if (currentTime > lastHour && current > currentMins) break;
    }

    result.push([formatTime(currentTime), 0, 0]);

    current += 60;

    // 防止无限循环（最多24小时）
    if (current - firstHour > 24 * 60) break;
  }

  return result;
}

// 获取夜班到当前时间补全
export const currentNightWork = function (startTime, endLimitTime) {
  const result = [];

  // 获取当前时间
  const now = new Date();
  const currentHours = now.getHours();
  const currentMinutes = now.getMinutes();
  const currentTimeStr = `${String(currentHours).padStart(2, '0')}:${String(currentMinutes).padStart(2, '0')}`;

  // 解析时间字符串为分钟数
  const parseTime = (timeStr) => {
    const [hours, minutes] = timeStr.split(':').map(Number);
    return hours * 60 + minutes;
  };

  // 格式化分钟数为HH:mm
  const formatTime = (minutes) => {
    const hrs = Math.floor(minutes / 60) % 24;
    return `${String(hrs).padStart(2, '0')}:00`;
  };

  const startMins = parseTime(startTime);
  const currentMins = parseTime(currentTimeStr);
  const endLimitMins = parseTime(endLimitTime);

  // 计算第一个整点时间（向上取整）
  let firstHour = startMins;
  if (startMins % 60 !== 0) {
    firstHour = startMins + (60 - startMins % 60);
  }

  // 确定结束时间（取当前时间和限制时间的较小值）
  const endMins = Math.min(currentMins, endLimitMins);
  const lastHour = endMins - (endMins % 60);

  // 生成时间点
  let current = firstHour;
  while (true) {
    // 处理跨天情况
    const currentTime = current % (24 * 60);

    // 添加当前时间点到结果数组
    result.push([formatTime(currentTime), 0, 0]);

    // 检查是否到达结束时间
    if (currentTime === lastHour) break;

    current += 60;

    // 防止无限循环（最多24小时）
    if (result.length >= 24) break;
  }

  return result;
}

// 返回整天的时间点
export const allDay = function () {
  const result = [];

  for (let hour = 0; hour < 24; hour++) {
    const formattedHour = String(hour).padStart(2, '0');
    result.push([`${formattedHour}:00`, 0, 0]);
  }

  return result;
}
// 返回当前天的时间点
export const currentAllDay = function () {
  const result = [];

  // 获取当前时间
  const now = new Date();
  const currentHour = now.getHours();
  const currentMinute = now.getMinutes();

  // 计算当前时间的整点（向下取整）
  const lastHour = currentMinute === 0 ? currentHour : currentHour;

  // 生成从00:00到当前整点的时间数组
  for (let hour = 0; hour <= lastHour; hour++) {
    const formattedHour = String(hour).padStart(2, '0');
    result.push([`${formattedHour}:00`, 0, 0]);
  }

  return result;
}



export const option1compare = {
  grid: {
    left: "3%",
    right: "3%",
    top: "12%",
    bottom: "9%",
  },
  tooltip: {
    trigger: "axis",
    formatter: function (params) {
      // params 是数组，包含所有系列的数据
      let result = params[0].axisValue + '<br>';
      params.forEach(function (item) {
        // 使用系列的颜色作为小圆点
        const color = item.color;
        // 根据系列名称添加单位
        const unit = ' t-CO₂'
        result += `<span style="display:inline-block;margin-right:5px;border-radius:10px;width:10px;height:10px;background-color:${color};"></span>`;
        result += ': ' + formatNumberText(item.value) + unit + '<br>';
      });
      return result;
    }
  },
  legend: {
    itemWidth: 40,
    // 单个图例项的图标高度
    itemHeight: 24,
    data: ["今年", '去年'],

    left: "center",
    textStyle: {
      color: "black", // 设置为红色
      // 还可以设置其他文字样式
      fontSize: 14,
      fontWeight: "bold",
    },
  },
  xAxis: {
    name: "小时",
    nameTextStyle: {
      color: "black",
      fontWeight: 600,
    },
    type: "category",
    axisTick: {
      show: false,
    },
    axisLabel: {
      color: "black",
      fontWeight: 600,
    },
    splitLine: {
      show: false, // 不显示分割线
    },
    data: [
      "00:00",
      "01:00",
      "02:00",
      "03:00",
      "04:00",
      "05:00",
      "06:00",
      "07:00",
      "08:00",
      "09:00",
      "10:00",
      "11:00",
      "12:00",
      "13:00",
      "14:00",
      "15:00",
      "16:00",
      "17:00",
      "18:00",
      "19:00",
      "20:00",
      "21:00",
      "22:00",
      "23:00",
    ],
  },
  yAxis: {
    name: "单位:Kw.h",
    nameTextStyle: {
      color: "black",
      fontWeight: 600,
    },
    type: "value",
    axisLine: {
      show: true,
    },
    axisLabel: {
      color: "black",
      fontWeight: 600,
      fontSize: 10,
    },
    splitLine: {
      show: false, // 不显示分割线
    },
  },

  series: [
    {
      name: "今年",
      data: [
        40, 26, 50, 80, 70, 60, 30, 40, 26, 50, 80, 40, 26, 50, 80, 70,
        60, 30, 40, 26, 50, 80, 50, 80,
      ],
      type: "bar",
      itemStyle: {
        color: "#2491DE",
      },
      barWidth: "100%",
      barMaxWidth: 40,
    },
    {
      name: "去年",
      data: [
        40, 26, 50, 80, 70, 60, 30, 40, 26, 50, 80, 40, 26, 50, 80, 70,
        60, 30, 40, 26, 50, 80, 50, 80,
      ],
      type: "bar",
      itemStyle: {
        color: "#58DEFF",
      },
      barWidth: "100%",
      barMaxWidth: 40,
    },
  ],
}


export const option1Qu = {
  grid: {
    left: "3%",
    right: "3%",
    top: "12%",
    bottom: "9%",
  },
  tooltip: {
    trigger: "axis",
    formatter: function (params) {
      // params 是数组，包含所有系列的数据
      let result = params[0].axisValue + '<br>';
      params.forEach(function (item) {
        // 使用系列的颜色作为小圆点
        const color = item.color;
        // 根据系列名称添加单位
        const unit = item.seriesName === '小时' || item.seriesName === '日' || item.seriesName === '月' ? ' t-CO₂' : '';
        result += `<span style="display:inline-block;margin-right:5px;border-radius:10px;width:10px;height:10px;background-color:${color};"></span>`;
        result += ': ' + formatNumberText(item.value) + unit + '<br>';
      });
      return result;
    }
  },
  legend: {
    itemWidth: 40,
    // 单个图例项的图标高度
    itemHeight: 24,
    data: ["小时"],

    left: "center",
    textStyle: {
      color: "black", // 设置为红色
      // 还可以设置其他文字样式
      fontSize: 14,
      fontWeight: "bold",
    },
  },
  xAxis: {
    name: "小时",
    nameTextStyle: {
      color: "black",
      fontWeight: 600,
    },
    type: "category",
    axisTick: {
      show: false,
    },
    axisLabel: {
      color: "black",
      fontWeight: 600,
    },
    splitLine: {
      show: false, // 不显示分割线
    },
    data: [
      "00:00",
      "01:00",
      "02:00",
      "03:00",
      "04:00",
      "05:00",
      "06:00",
      "07:00",
      "08:00",
      "09:00",
      "10:00",
      "11:00",
      "12:00",
      "13:00",
      "14:00",
      "15:00",
      "16:00",
      "17:00",
      "18:00",
      "19:00",
      "20:00",
      "21:00",
      "22:00",
      "23:00",
    ],
  },
  yAxis: {
    name: "单位:Kw.h",
    nameTextStyle: {
      color: "black",
      fontWeight: 600,
    },
    type: "value",
    axisLine: {
      show: true,
    },
    axisLabel: {
      color: "black",
      fontWeight: 600,
      fontSize: 10,
    },
    splitLine: {
      show: false, // 不显示分割线
    },
  },

  series: [
    {
      name: "小时",
      data: [
        40, 26, 50, 80, 70, 60, 30, 40, 26, 50, 80, 40, 26, 50, 80, 70,
        60, 30, 40, 26, 50, 80, 50, 80,
      ],
      type: "bar",
      itemStyle: {
        color: "#2491DE",
      },
      barWidth: "100%",
      barMaxWidth: 40,
    },
  ],
}
export const ArrEachOne = function (arr, arr1) {
  let res = []
  arr.forEach(item => {
    arr1.push(item[1])
  });

}


// 全部设备的搜索

